package com.tusuo.stds.util;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by developer on 17-6-12.
 */

public class CustomViewPager extends ViewPager {

    //手指上一次在屏幕上的点为(lastX1, y1)手指这一次在屏幕上的点为(currentX1, y2)
    private float lastX1 = 0;
    private float currentX1 = 0;
    private float moveX1 = 0;
    private float lastX2 = 0;
    private float currentX2 = 0;
    private float moveX2 = 0;

    private static boolean mRightSwipe = false;
    private static boolean mLeftSwipe = false;

    private Context context;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public CustomViewPager(Context context) {
        super(context);
        this.context = context;
    }

    public void enableRightSwipe(boolean rightSwipe) {
        mRightSwipe = rightSwipe;
    }

    public void enableLeftSwipe(boolean leftSwipe) {
        mLeftSwipe = leftSwipe;
    }

    public boolean getEnableLeftSwipe() {
        return mLeftSwipe;
    }

    public boolean getEnableRightSwipe() {
        return mRightSwipe;
    }
    @Override
    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN :
                lastX2 = ev.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                currentX2 = ev.getX();
                moveX2 = currentX2 - lastX2;

                if (moveX2 > 0) {
                    if (!mLeftSwipe) {
                        return false;
                    }
                }else {
                    if (!mRightSwipe) {
                        return false;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {


        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN :
                lastX1 = ev.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                currentX1 = ev.getX();
                moveX1 = currentX1 - lastX1;

                if (moveX1 > 0) {
                    if (!mLeftSwipe) {
                        return false;
                    }
                }else {
                    if (!mRightSwipe) {
                        return false;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:

                break;
        }

        return super.onInterceptTouchEvent(ev);
    }

    /*@Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
//            lastX1 = 0;
        }

        if(motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
            if (lastX1 == 0) {
                lastX1 = motionEvent.getX();
                return false;
            }

            if (lastX1 != motionEvent.getX()) {
                currentX1 = motionEvent.getX();

                if(currentX1 - lastX1 > 5) {
                    if (!mRightSwipe) {
                        return false;
                    }
                } else if (currentX1 - lastX1 < -5) {
                    if (!mLeftSwipe) {
                        return false;
                    }
                }

                lastX1 = motionEvent.getX();
            }

            return super.onTouchEvent(motionEvent);
        }

        return super.onTouchEvent(motionEvent);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {

        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
//            lastX1 = 0;
        }

        if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
            if (lastX1 == 0) {
                lastX1 = motionEvent.getX();
                return false;
            }

            if (lastX1 != motionEvent.getX()) {
                currentX1 = motionEvent.getX();

                if (currentX1 - lastX1 > 50) {
//                    Toast.makeText(context, "向右滑", Toast.LENGTH_SHORT).show();
                    if (!mRightSwipe) {
                        int x = 1;
                        return false;
                    }
                } else if (currentX1 - lastX1 < -50) {
//                    Toast.makeText(context, "向左滑", Toast.LENGTH_SHORT).show();
                    if (!mLeftSwipe) {
                        int x = 1;
                        return false;
                    }
                }

                lastX1 = motionEvent.getX();
            }

            return super.onInterceptTouchEvent(motionEvent);
        }

        return super.onInterceptTouchEvent(motionEvent);
    }*/

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        super.setCurrentItem(item, smoothScroll);
    }

    @Override
    public void setCurrentItem(int item) {
        super.setCurrentItem(item);
    }
}